<?php

namespace app\api\controller;

use ba\Tree;
use hehe\Network;
use Throwable;
use think\facade\Db;
use think\facade\Cookie;
use think\facade\Config;
use app\common\controller\Frontend;

class Api extends Frontend
{
    protected array $noNeedLogin = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 保存每日汇率
     */
    public function exchange(){
        $url = "http://web.juhe.cn/finance/exchange/rmbquot"; // 接口请求URL
        $data = [
            'key' => 'ae127442739168f1b5d73f77a638a9a7',
            'type'=> '1',
        ];
        $result = Network::httpCurl($url, http_build_query($data));
        $result = json_decode($result, true);
        $data = $result['result'][0];
        $insert = [];
        foreach($data as $val){
            $val['create_time'] = $this->timestamp;
            $insert[] = $val;
        }
        $result = db::name('exchange_record')->insertAll($insert);

        if($result){
            echo $val['date'] . ' 汇率信息写入成功';
        }else{
            echo $val['date'] . ' 汇率信息写入失败';
        }
        die;
    }

}